import {
	getCustomerInfoByToken,
} from '@/api/login.js';
import {
	salesmanInfo
} from "@/api/userSalesman.js";

import {
	encryptionNew
} from '../utils/encryption.js'
import {
	reportBaseUrl
} from '../config/tendIdConfig.js'
import {
	getShippingAddress,
} from "@/api/shop.js";

// import {
// 	quickLogin
// } from '@/api/health_service.js'

const actions = {
	GET_ADDRESS_LIST({
		state,
		commit
	}, force) {
		return new Promise(reslove => {
			getShippingAddress({
					pageNo: 1,
					pageSize: 1000
				})
				.then((res) => {
					if (res.code === 200) {
						commit('SET_ADDRES_LIST', res?.data?.rows ?? [])
					}
				})



		}).catch(() => {

		});
	},

	USERINFO({
		state,
		commit,
	}, payload) {
		// console.log(payload)
		return new Promise((resolve, reject) => {
			if (state.authority === 'customer') {
				// 客户端 用户信息
				getCustomerInfoByToken().then(res => {
					// 获取到用户信息后登录健康平台
					const params = {
						...encryptionNew({
							userPhone: res.data.mobile,
							appKey: process.env.NODE_ENV === 'development' ? 'test' :
								'app65026a842ab4e',
							fullName: '',
						})

					}
					// 因为vuex中无法直接访问Vu 实例上挂载的插件。如:uView 中的 uni.$u 这里需要在调用该action的时候通过外部将$u给传进来才能使用
					payload.$u.http.post(`/web/open/quickLogin`, params, {
						custom: {
							auth: false
						}
					}).then(healthLoginResult => {
						commit("SET_TOKEN_HEALTH_PLATFORM", healthLoginResult.accessToken);
						commit("SET_HEALTH_PLATFORM_USERINFO", healthLoginResult);
					})

					commit("SET_USERINFO", res.data);
					res?.data?.mobile && commit("SET_PHONE", res?.data?.mobile);
					resolve(res.data);
				}).catch(reject);
			} else if (state.authority === 'salesman') {
				// 业务员 用户信息
				salesmanInfo().then(res => {
					commit("SET_USERINFO", res.data);
					res?.data?.mobile && commit("SET_PHONE", res?.data?.mobile);
					resolve(res.data);
				}).catch(reject);
			} else {
				// 默认情况，尝试获取客户信息
				getCustomerInfoByToken().then(res => {
					commit("SET_USERINFO", res.data);
					res?.data?.mobile && commit("SET_PHONE", res?.data?.mobile);
					resolve(res.data);
				}).catch(reject);
			}
		}).catch(() => {
			// 错误处理
		});
	},

	REPORT_LOGIN({
		state,
		commit
	}, param) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: `${reportBaseUrl}/web/open/quickLogin`,
				method: 'POST',
				// responseType: 'text',
				data: {
					...encryptionNew({
						userPhone: param.customerPhone,
						appKey: process.env.NODE_ENV === 'development' ? 'test' :
							'app65026a842ab4e',
						fullName: '',
					})
				},
				success(result) {
					if (result.data.code === 200) {
						commit('SET_REPORT_TOKEN', result.data.data.accessToken)
						commit('SET_REPORT_INFO', result.data.data)
						resolve(result.data.data)
					} else {
						uni.showToast({
							title: result.data.msg,
							icon: 'none'
						})
						commit('REMOVE_REPORT_INFO');
						commit('REMOVE_REPORT_TOKEN');
						reject(result.data)
					}

					if (result.data.code === 992) {
						uni.redirectTo({
							url: '/pages_my/health_records/index'
						})
					}
				},
				fail(error) {
					uni.showToast({
						title: '请求失败',
						icon: 'none'
					})
					reject(error)
				}
			})
		})
	}
}
export default actions
